package com.hula.onion.book.server.controller;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by shan on 18/1/24.
 */
@Api(value = "用户注册、登陆相关")
@RestController
@RequestMapping("/onion-book/api/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "微信或QQ注册/登录")
    @RequestMapping(value = "/openIdLogin", method = RequestMethod.POST)
    public CommonResponse openIdLogin(@RequestParam String nickName,
                                      @RequestParam String headPic,
                                      @RequestParam String openId,
                                      @RequestParam int channelId) {
        return userService.openIdLogin(nickName, headPic, openId, channelId);
    }

    @ApiOperation(value = "微信小程序登陆")
    @RequestMapping(value = "/littleWxLogin", method = RequestMethod.POST)
    public CommonResponse littleWxLogin(@RequestParam String code) {
        return userService.littleWxLogin(code);
    }

    @ApiOperation(value = "绑定第三方账号（手机号注册账户，未绑定微信或者qq时调用）")
    @RequestMapping(value = "/bindThird", method = RequestMethod.POST)
    public CommonResponse bindThird(@RequestParam String nickName,
                                    @RequestParam String headPic,
                                    @RequestParam String openId,
                                    @RequestParam Integer userId,
                                    @RequestParam int channelId) {
        return userService.bindThird(userId, openId,
                nickName, headPic, channelId);
    }

    @ApiOperation(value = "绑定手机号(微信登录用户,未绑手机时调用)")
    @RequestMapping(value = "/bindPhone", method = RequestMethod.POST)
    public CommonResponse bindPhone(@RequestParam String verifyCode,
                                    @RequestParam String mobile,
                                    @RequestParam String password,
                                    @RequestParam int userId) {
        return userService.bindPhone(userId, verifyCode, mobile, password);
    }

    @ApiOperation(value = "获取验证码(codeType:1-注册/绑定手机号;2-修改密码;3-快捷登陆)")
    @RequestMapping(value = "/get/verifyCode", method = RequestMethod.GET)
    public CommonResponse getVerifyCode(@RequestParam String mobile,
                                        @RequestParam int codeType,
                                        @RequestParam String md5Code) {
        return userService.sendVerifyCode(mobile, codeType, md5Code);
    }

    @ApiOperation(value = "手机号注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public CommonResponse register(@RequestParam String verifyCode,
                                   @RequestParam String mobile,
                                   @RequestParam String password) {
        return userService.register(verifyCode, mobile, password);
    }

    @ApiOperation(value = "手机密码登陆")
    @RequestMapping(value = "/pwdLogin", method = RequestMethod.GET)
    public CommonResponse pwdLogin(@RequestParam String mobile,
                                   @RequestParam String password) {
        return userService.pwdLogin(mobile, password);
    }

    @ApiOperation(value = "手机验证码快捷登陆")
    @RequestMapping(value = "/codeLogin", method = RequestMethod.GET)
    public CommonResponse codeLogin(@RequestParam String mobile,
                                    @RequestParam String verifyCode) {
        return userService.codeLogin(mobile, verifyCode);
    }

    @ApiOperation(value = "忘记密码")
    @RequestMapping(value = "/forget/password", method = RequestMethod.POST)
    public CommonResponse forgetPassword(@RequestParam String verifyCode,
                                         @RequestParam String mobile,
                                         @RequestParam String newPassword) {
        return userService.forgetPassword(verifyCode, mobile, newPassword);
    }

    @ApiOperation(value = "修改密码")
    @RequestMapping(value = "/change/password", method = RequestMethod.POST)
    public CommonResponse changeLoginPassword(@RequestParam String oldPassword,
                                              @RequestParam String newPassword,
                                              @RequestParam int userId) {
        return userService.changePassword(userId, oldPassword, newPassword);
    }

    @ApiOperation(value = "用户意见反馈")
    @RequestMapping(value = "/createFeedBack", method = RequestMethod.POST)
    public CommonResponse createFeedBack(@RequestParam int userId,
                                         @RequestParam String message) {
        return userService.createFeedBack(userId, message);
    }
}
